<?php
namespace app\admin\controller;
use app\admin\code\BaseCode;
use app\admin\logic\EngineeringLogic;
use app\admin\model\EngineeringModel;
use app\admin\util\Page;
use app\admin\util\Response;
use app\admin\validate\EngineeringValidate;
use think\Loader;
use think\Request;

class BaseMessage extends Base
{
    protected $_model;
    protected $_like_model;
    protected $_comment_model;
    protected $_controller;
    protected $_template_index;
    protected $_template_detail;
    protected $_size = 10;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->_template_index = 'base_message/index';
        $this->_template_detail = 'base_message/oppt';
        $this->_controller = Request::instance()->controller();
        $this->_model = Loader::model($this->_controller,'model',true,'admin');
    }


    public function del(){
        $param = [];
        $param['id'] = input('id',0,'intval');
        $class = new \ReflectionClass('app\\admin\\logic\\'.$this->_controller.'Logic');
        $logic = $class->newInstance($param);
        $class = new \ReflectionClass('app\\admin\\validate\\'.$this->_controller.'Validate');
        if($logic->del($class->newInstance($this->_model))){
            Response::json(BaseCode::SUCCESS);
        }else{
            Response::json($logic->err['code'],$logic->err['msg']);
        }
    }
    public function detail(){
        $id = input('id',0,'intval');
        $vo = $this->_model->where(['id'=>$id])->find();
        $vo['like_num'] = $this->_get_like_num($vo['id']);
        $vo['comment_num'] = $this->_get_comment_num($vo['id']);
        $this->assign('vo',$vo);
        return view($this->_template_detail);
    }
    public function update_status(){
        $id = input('id',0,'intval');
        $status = input('status',0,'intval');
        $param = [];
        $param['id'] = $id;
        $param['status'] = $status;
        $class = new \ReflectionClass('app\\admin\\logic\\'.$this->_controller.'Logic');
        $logic = $class->newInstance($param);
        $class = new \ReflectionClass('app\\admin\\validate\\'.$this->_controller.'Validate');
        if($logic->update_status($class->newInstance($this->_model))){
            Response::json(BaseCode::SUCCESS);
        }else{
            Response::json($logic->err['code'],$logic->err['msg']);
        }
    }

    protected function _get_comment_num($id)
    {
        if ($this->_comment_model) {
            return $this->_comment_model->where(['message_id' => $id])->count();
        } else {
            return 0;
        }
    }

    protected function _get_like_num($id)
    {
        if ($this->_like_model) {
            return $this->_like_model->where(['message_id' => $id])->count();
        } else {
            return 0;
        }
    }
}